<html>
    <head>
        <title>Itowns - Split</title>

        <style type="text/css">
           #splitSlider {
               position: absolute;
               left: 50%;
               top: 0px;
               width: 4px;
               height: 100%;
               background-color: #666666;
           }

           #splitSlider:hover {
               cursor: ew-resize;
           }
        </style>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/example.css">
        <link rel="stylesheet" type="text/css" href="css/loading_screen.css">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="js/GUI/dat.gui/dat.gui.min.js"></script>
    </head>
    <body>
        <div id="viewerDiv"></div>
        <div id="splitSlider"></div>

        <script src="../dist/itowns.js"></script>
        <script src="js/loading_screen.js"></script>
        <script type="text/javascript">
            // # Simple Globe viewer

            // Define initial camera position
            var positionOnGlobe = { longitude: 3.36, latitude: 51.22, altitude: 480000 };

            // `viewerDiv` will contain iTowns' rendering area (`<canvas>`)
            var viewerDiv = document.getElementById('viewerDiv');

            // Instanciate iTowns GlobeView*
            var view = new itowns.GlobeView(viewerDiv, positionOnGlobe);

            var promises = [];

            var orthoLayer;
            var osmLayer;
            var splitSlider;
            var splitPosition;
            var xD;

            setupLoadingScreen(viewerDiv, view);

            // Add two imagery layers to the scene
            // This layer is defined in a json file but it could be defined as a plain js
            // object. See Layer* for more info.
            function addColorLayerFromConfig(config) {
                var layer = new itowns.ColorLayer(config.id, config);
                return view.addLayer(layer);
            }
            promises.push(itowns.Fetcher.json('./layers/JSONLayers/Ortho.json')
                .then(function _(c) {
                    c.source = new itowns.WMTSSource(c.source);
                    return c;
                })
                .then(addColorLayerFromConfig)
                .then(function _(l) { orthoLayer = l; }));
            promises.push(itowns.Fetcher.json('./layers/JSONLayers/OPENSM.json')
                .then(function _(c) {
                    c.source = new itowns.TMSSource(c.source);
                    return c;
                })
                .then(addColorLayerFromConfig)
                .then(function _(l) { osmLayer = l; }));

            // Add two elevation layers.
            // These will deform iTowns globe geometry to represent terrain elevation.
            function addElevationLayerFromConfig(config) {
                config.source = new itowns.WMTSSource(config.source);
                var layer = new itowns.ElevationLayer(config.id, config);
                view.addLayer(layer);
            }
            itowns.Fetcher.json('./layers/JSONLayers/WORLD_DTM.json').then(addElevationLayerFromConfig);
            itowns.Fetcher.json('./layers/JSONLayers/IGN_MNT_HIGHRES.json').then(addElevationLayerFromConfig);

            // Slide handling
            splitPosition = 0.5 * window.innerWidth;
            xD = 0;
            function splitSliderMove(evt) {
                var s = (evt.clientX - xD) / splitSlider.parentElement.offsetWidth;
                splitSlider.style.left = (100.0 * s) + '%';
                splitPosition = s * window.innerWidth;
                view.notifyChange();
            }

            function mouseDown(evt) {
                xD = evt.clientX - splitSlider.offsetLeft;
                window.addEventListener('mousemove', splitSliderMove, true);
            }

            function mouseUp() {
                window.removeEventListener('mousemove', splitSliderMove, true);
            }

            function changeLayerVisibility(orthoVisible, osmVisible) {
                view.scene.traverse(function _(obj) {
                    if (obj.material && obj.material.visible && obj.material.getLayer) {
                        var ortho = obj.material.getLayer(orthoLayer.id);
                        var osm = obj.material.getLayer(osmLayer.id);
                        if (ortho) ortho.visible = orthoVisible;
                        if (osm) osm.visible = osmVisible;
                    }
                });
            }
            splitSlider = document.getElementById('splitSlider');
            document.getElementById('splitSlider').addEventListener('mousedown', mouseDown, false);
            window.addEventListener('mouseup', mouseUp, false);

            // Rendering code
            function splitRendering() {
                var g = view.mainLoop.gfxEngine;
                var r = g.renderer;

                r.setScissorTest(true);

                // render ortho layer on the left
                changeLayerVisibility(true, false);

                r.setScissor(0, 0, splitPosition + 2, window.innerHeight);
                g.renderView(view);

                // render osm layer on the right
                changeLayerVisibility(false, true);

                r.setScissor(splitPosition + 2, 0, window.innerWidth - splitPosition - 2, window.innerHeight);
                g.renderView(view);
            }

            // Override default rendering method when color layers are ready
            Promise.all(promises).then(
                function _() { view.render = splitRendering; }).catch(console.error);
        </script>
    </body>
</html>
